package com.qm.web;



import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.jws.WebParam.Mode;
import javax.servlet.http.HttpSession;



import com.qm.model.Classes;
import com.qm.model.Course;
import com.qm.model.Score;
import com.qm.model.Topic;
import com.qm.model.TopicTest;
import com.qm.model.Users;
import com.qm.model.UsersClass;
import com.qm.service.*;

import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;


@RequestMapping("/user")
@Controller
public class UserController {
	@Autowired
	private UserService uservice;
	@Autowired
	private AdminCourseService acourse;
	/**
	 * 前台页面的主页
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView showIndex(){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("/front/Users/success");
		return mav;
	};

	
	/**
	 * 在线考试
	 * @return
	 */
	@RequestMapping("topicShow/{course_id}")
	public ModelAndView topicShow(@PathVariable("course_id")Integer courseId, Integer currentCid,HttpSession session)
	{
		System.out.println("==========================="+currentCid);
		ModelAndView mav=new ModelAndView();
		ArrayList<Course> courseName=acourse.selectCourseName();//选择数据库中的课程信息
		mav.addObject("courseName", courseName);
		mav.setViewName("/front/Users/Topic");
		if(courseId>0){
			HashMap<Integer,ArrayList<TopicTest>> map=(HashMap<Integer, ArrayList<TopicTest>>) session.getAttribute("courseTestMap");
			if(map==null){
				map=new HashMap<>();
				session.setAttribute("courseTestMap", map);
			}
			ArrayList<TopicTest> tpList=map.get(courseId);
			if(tpList==null){
				tpList=uservice.onlineExam(courseId);
				map.put(courseId, tpList);
			}
			mav.addObject("topicList", tpList);
			mav.addObject("cocourseCode", courseId);//当前课程代码
			mav.addObject("cid", currentCid);//当前班级名称
		}
		return mav;
	
	}
	/**
	 * 通过选择课程来选择班级
	 * @param cocourseCode
	 * @return
	 */
	@ResponseBody
	@RequestMapping("selectClasses")
	public ArrayList<Classes> selectClasses(int cocourseCode){
		ArrayList<Classes> classes =acourse.selectClasses(cocourseCode);
		return classes;
	}
	/**
	 * 答案查询，插入成绩
	 * @param courseId
	 * @param ansIndexList
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("submitAnswer")
	public int submitTopic(Integer courseId,Integer classId,@RequestParam("ansIndexList[]") ArrayList<Integer> ansIndexList,HttpSession session){
		HashMap<Integer,ArrayList<TopicTest>> map=(HashMap<Integer, ArrayList<TopicTest>>) session.getAttribute("courseTestMap");
		if(map==null){
			return -1;
		}
		ArrayList<TopicTest> tpList=map.get(courseId);
		if(tpList==null){//如果试题为空
			return -2;
		}
		if(ansIndexList.size()!=tpList.size()){//如果传过来的答案和正确答案的数目不匹配
			return -3;
		}
		ArrayList<TopicTest> mistakeList=new ArrayList<>();//错误题数组
		int score=0;
		for(int i=0;i<ansIndexList.size();i++){
			TopicTest tp=tpList.get(i);
			Integer ansIndex=ansIndexList.get(i);
			if(ansIndex!=tp.getTcorrectIndex()){
				mistakeList.add(tp);
			}else{
				score+=5;
			}
		}
		Users u=(Users)session.getAttribute("user");//在session中获取用户信息
		int uid=u.getUid();//获取用户ID
		UsersClass uc=new UsersClass();
		uc.setUcscore(score);
		uc.setUcuid(uid);
		uc.setUccid(classId);
		uservice.insertScoreByUid(uc);
		HashMap<Integer,ArrayList<TopicTest>> map2=(HashMap<Integer, ArrayList<TopicTest>>) session.getAttribute("mistakeMap");
		if(map2==null){
			map2=new HashMap<>();
			session.setAttribute("mistakeMap", map2);//将错误答案序列放进map中
		}
		map2.put(courseId, mistakeList);
		return score;
	}
	/**
	 * 显示错误题页面
	 * @return
	 */
	@RequestMapping("WrongTitlePage")
	public ModelAndView WrongTitlePage(Integer courseId,HttpSession session){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("/front/Users/WrongTitlePage");
		HashMap<Integer,ArrayList<TopicTest>> map2=(HashMap<Integer, ArrayList<TopicTest>>) session.getAttribute("mistakeMap");
		mav.addObject("wrongTitile",map2.get(courseId));
		return mav;
	}
	/**
	 * 成绩查询
	 * @return
	 */
	@RequestMapping("QueryResults")
	public ModelAndView QueryResults(HttpSession session){
		ModelAndView mav=new ModelAndView();
		ArrayList<Score> result=new ArrayList<>();
		Users u=(Users)session.getAttribute("user");
		int uid=u.getUid();
		result=uservice.selectScoreByUid(uid);
		mav.addObject("studentScore", result);
		mav.setViewName("/front/Users/QueryResults");
		return mav;
		
	}
	
 }
